﻿<template>
	<div class="card-fill layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div>
				<el-form ref="formRefProcessRoute" :model="stateProcessRoute.vmModel" label-width="100px">
					<el-row>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_RoutingName_FormItem" prop="Entity.RoutingName"
								:label="$t('message.autotrans._Model_ProcessRoute_RoutingName')" :rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_ProcessRoute_RoutingName') }),
										trigger: 'blur',
									},
								]">
								<el-input v-model="stateProcessRoute.vmModel.Entity.RoutingName" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_RoutingCode_FormItem" prop="Entity.RoutingCode"
								:label="$t('message.autotrans._Model_ProcessRoute_RoutingCode')" :rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_ProcessRoute_RoutingCode') }),
										trigger: 'blur',
									},
								]">
								<el-input v-model="stateProcessRoute.vmModel.Entity.RoutingCode" clearable></el-input>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_LineId_FormItem" prop="Entity.LineId" :rules="[
								{
									required: true,
									message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_ProcessRoute_LineId') }),
									trigger: 'blur',
								},
							]" :label="$t('message.autotrans._Model_ProcessRoute_LineId')">
								<el-select v-model="stateProcessRoute.vmModel.Entity.LineId"
									@change="OnProductionLineChange" clearable>
									<el-option v-for="item in stateProcessRoute.AllProductLines" :key="item.Value"
										:value="Number(item.Value)" :label="item.Text"></el-option>
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_ProductId_FormItem" prop="Entity.ProductId" :rules="[
								{
									required: true,
									message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_ProcessRoute_ProductId') }),
									trigger: 'blur',
								},
							]" :label="$t('message.autotrans._Model_ProcessRoute_ProductId')">
								<el-select v-model="stateProcessRoute.vmModel.Entity.ProductId" @change="OnProductChange" clearable>
									<el-option v-for="item in stateProcessRoute.AllProducts" :key="item.Value"
										:value="Number(item.Value)" :label="item.Text"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<!--        <el-col :xs="24" :lg="24" class="mb20">-->
						<!--        <el-form-item ref="Entity_ProductCode_FormItem" prop="Entity.ProductCode" :label="$t('message.autotrans._Model_ProcessRoute_ProductCode')"            :rules="[{required: true, message: $t('message._system.common.vm.input',{input:$t('message.autotrans._Model_ProcessRoute_ProductCode')}), trigger: 'blur' }]" >-->
						<!--            <el-input v-model="stateProcessRoute.vmModel.Entity.ProductCode" clearable></el-input>-->
						<!--        </el-form-item>-->

						<!--        </el-col>-->
						<!--        <el-col :xs="24" :lg="24" class="mb20">-->
						<!--        <el-form-item ref="Entity_ProductName_FormItem" prop="Entity.ProductName" :label="$t('message.autotrans._Model_ProcessRoute_ProductName')">-->
						<!--            <el-input v-model="stateProcessRoute.vmModel.Entity.ProductName" clearable></el-input>-->
						<!--        </el-form-item>-->

						<!--        </el-col>-->
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_Active_FormItem" prop="Entity.Active"
								:label="$t('message.autotrans._Model_ProcessRoute_Active')" :rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_ProcessRoute_Active') }),
										trigger: 'blur',
									},
								]">
								<el-switch v-model="stateProcessRoute.vmModel.Entity.Active" clearable></el-switch>
							</el-form-item>
						</el-col>
						<!--        <el-col :xs="24" :lg="24" class="mb20">-->
						<!--        <el-form-item ref="Entity_Aux_1_FormItem" prop="Entity.Aux_1" :label="$t('message.autotrans._Model_ProcessRoute_Aux_1')">-->
						<!--            <el-input v-model="stateProcessRoute.vmModel.Entity.Aux_1" clearable></el-input>-->
						<!--        </el-form-item>-->

						<!--        </el-col>-->
						<!--        <el-col :xs="24" :lg="24" class="mb20">-->
						<!--        <el-form-item ref="Entity_Aux_2_FormItem" prop="Entity.Aux_2" :label="$t('message.autotrans._Model_ProcessRoute_Aux_2')">-->
						<!--            <el-input v-model="stateProcessRoute.vmModel.Entity.Aux_2" clearable></el-input>-->
						<!--        </el-form-item>-->

						<!--        </el-col>-->
					</el-row>

					<WtmTable ref="tableRefProcessRouteItemRoutieList" v-bind="tableDataProcessRouteItemRoutieList"
						v-model="stateProcessRoute.vmModel.Entity.ProcessRouteItem_Routie"></WtmTable>
					<div style="text-align: right">
						<WtmButton @click="onSubmitProcessRoute" type="primary"
							:button-text="$t('message.autotrans.SysSubmit')" style="margin-top: 15px" />

						<WtmButton @click="onCloseProcessRoute" type="primary"
							:button-text="$t('message.autotrans.SysClose')" style="margin-top: 15px" />
					</div>
				</el-form>
			</div>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message.autotrans._PageProcessManagementProcessRouteCreate;false">
import { ElMessageBox, ElMessage, ElLoading } from 'element-plus';
import { defineAsyncComponent, reactive, ref, getCurrentInstance, onMounted, nextTick, onBeforeMount } from 'vue';
import { processrouteApi } from '/@/api/processmanagement/processroute';
import other from '/@/utils/other';
import fileApi from '/@/api/file';
import { useRouter } from "vue-router";
import { watch } from 'fs';
const ci = getCurrentInstance() as any;


// 定义变量内容
const tableRefProcessRouteItemRoutieList = ref();

// 定义变量内容
const formRefProcessRoute = ref();
const stateProcessRoute = reactive({
	vmModel: {
		Entity: {
			RoutingName: '',
			RoutingCode: '',
			ProductId: null,
			ProductCode: '',
			ProductName: '',
			LineId: null,
			Active: true,
			Aux_1: '',
			Aux_2: '',
			ProcessRouteItem_Routie: [],
		},

	},
	AllEquipments: [] as any[],
	AllProducts: [] as any[],
	AllRouties: [] as any[],
	AllProductLines: [] as any[],
});




const tableDataProcessRouteItemRoutieList = ref({
	// 列表数据（必传）
	data: [],
	// 表头内容（必传，注意格式）
	header: [
		{ title: ci.proxy.$t('message.autotrans.Page工艺位置'), key: 'Pos', type: 'number', isCheck: true, sortable: 'custom', colWidth: 180 },
		{ title: ci.proxy.$t('message.autotrans.Page关联设备'), key: 'EquipmentId', type: 'combobox', colWidth: 180, comboData: `/api/ProcessManagement/ProcessRoute/GetEquipmentByProductionLine?id=${stateProcessRoute.vmModel.Entity.LineId}`, isCheck: true },
		{ title: ci.proxy.$t('message.autotrans.Page工序组'), key: 'GroupCode', type: 'textbox', isCheck: true, colWidth: 120 },
		{ title: ci.proxy.$t('message.autotrans.Page工序编号'), key: 'OpCode', type: 'textbox', isCheck: true, colWidth: 120 },
		{ title: ci.proxy.$t('message.autotrans.Page工序名称'), key: 'OpName', type: 'textbox', isCheck: true, colWidth: 120 },
		{
			title: ci.proxy.$t('message.autotrans.Page工序类型'), key: 'OpType', type: 'combobox', colWidth: 120, comboData: {
				Assembling: ci.proxy.$t('message.autotrans._Enum_OpTypeEnum_Assembling'),
				Detection: ci.proxy.$t('message.autotrans._Enum_OpTypeEnum_Detection'),
				Rework: ci.proxy.$t('message.autotrans._Enum_OpTypeEnum_Rework'),
				TakePartInOrOut: ci.proxy.$t('message.autotrans._Enum_OpTypeEnum_TakePartInOrOut'),
				Weild: ci.proxy.$t('message.autotrans._Enum_OpTypeEnum_Weild'),
				PrintMark: ci.proxy.$t('message.autotrans._Enum_OpTypeEnum_PrintMark'),
				Fds: ci.proxy.$t('message.autotrans._Enum_OpTypeEnum_Fds'), isCheck: true
			}
		},
		{ title: ci.proxy.$t('message.autotrans.Page工厂工序'), key: 'ErpOpCode', type: 'textbox', isCheck: true, colWidth: 120 },
		{ title: ci.proxy.$t('message.autotrans.Page标准加工工时'), key: 'StdOpDuration', type: 'number', isCheck: true, colWidth: 120 },
		{ title: ci.proxy.$t('message.autotrans.Page标准准备工时'), key: 'StdPrepDuration', type: 'number', isCheck: true, colWidth: 120 },
		{
			title: ci.proxy.$t('message.autotrans.Page校验上一工序动作'), key: 'PolicyCheckPrevProc', type: 'combobox', colWidth: 120, comboData: {
				None: ci.proxy.$t('message.autotrans._Enum_PolicyCheckPrevProcEnum_None'),
				Worked: ci.proxy.$t('message.autotrans._Enum_PolicyCheckPrevProcEnum_Worked'),
				WorkedAndOK: ci.proxy.$t('message.autotrans._Enum_PolicyCheckPrevProcEnum_WorkedAndOK'),
			}, isCheck: true
		},
		{
			title: ci.proxy.$t('message.autotrans.Page校验生产BOM动作'), key: 'PolicyCheckProcBom', type: 'combobox', colWidth: 120, comboData: {
				None: ci.proxy.$t('message.autotrans._Enum_PolicyCheckProcBomEnum_None'),
				Over: ci.proxy.$t('message.autotrans._Enum_PolicyCheckProcBomEnum_Over'),
			}, isCheck: true
		},
		{
			title: ci.proxy.$t('message.autotrans.Page工艺校验策略'), key: 'PolicyCheckProcess', type: 'combobox', colWidth: 120, comboData: {
				None: ci.proxy.$t('message.autotrans._Enum_PolicyCheckProcBomEnum_None'),
				AllowedEntryRepeat: ci.proxy.$t('message.autotrans._Enum_PolicyCheckProcessEnum_AllowedEntryRepeat'),
				CheckBOMSource: ci.proxy.$t('message.autotrans._Enum_PolicyCheckProcessEnum_CheckBOMSource'),
			}, isCheck: true
		},
		{
			title: ci.proxy.$t('message.autotrans.Page工艺管控属性'), key: 'CtlAttr', type: 'combobox', colWidth: 120, comboData: {
				Must: ci.proxy.$t('message.autotrans._Enum_ProcCtlAttrEnum_Must'),
				Optional: ci.proxy.$t('message.autotrans._Enum_ProcCtlAttrEnum_Optional'),
			}, isCheck: true
		},

		{ title: ci.proxy.$t('message.autotrans.Page处理标记'), key: 'Etag', type: 'textbox', isCheck: true, colWidth: 120 },
		{
			title: ci.proxy.$t('message.autotrans.Page激活'), key: 'Active', type: 'switch', defaultValue: true, comboData:
			{
				true: ci.proxy.$t('message._system.common.vm.tips_bool_true'),
				false: ci.proxy.$t('message._system.common.vm.tips_bool_false')
			}, isCheck: true
		},

		// {title:ci.proxy.$t('message.autotrans.Page设备编号'),key: 'EquipmentCode',type: 'textbox',isCheck: true},

	],
	// 配置项（必传）
	config: {
		total: 0, // 列表总数
		loading: false, // loading 加载
		isSerialNo: true, // 是否显示表格序号
		isSelection: true, // 是否显示表格多选
		isOperate: true, // 是否显示表格操作栏
		isSub: true,
		isDisabled: false
	}
});




// 取消
const onCloseProcessRoute = () => {
	closeDialog();
};

// 提交
const onSubmitProcessRoute = () => {
	const loadingInstance = ElLoading.service({
		lock: true,
		text: '正在提交中，请等待。。。',
		background: 'rgba(0, 0, 0, 0.7)',
	});
	formRefProcessRoute.value?.validate((valid: boolean, fields: any) => {
		if (valid) {
			processrouteApi().add(stateProcessRoute.vmModel).then(() => {
				ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
				emit('refresh');
				closeDialog();
			}).catch((error) => {
				other.setFormError(ci, error);
			}).finally(() => {
				loadingInstance.close();
			})
		}
		else {
			loadingInstance.close();
		}
	})
};


//选择产线
const OnProductionLineChange = (value: any) => {
	if (value) {

		other.getSelectList(`/api/ProcessManagement/ProcessRoute/GetProductByProductionLine?id=${value}`, [], false).then(x => { stateProcessRoute.AllProducts = x });


		other.getSelectList(`/api/ProcessManagement/ProcessRoute/GetEquipmentByProductionLine?id=${value}`, [], false).then(x => { stateProcessRoute.AllEquipments = x });

		if (tableRefProcessRouteItemRoutieList.value) {
			// 1. 更新header中的comboData URL
			const equipmentCol = tableDataProcessRouteItemRoutieList.value.header.find(item => item.key === 'EquipmentId');
			if (equipmentCol) {
				equipmentCol.comboData = `/api/ProcessManagement/ProcessRoute/GetEquipmentByProductionLine?id=${value}`;
			}
			// 2. 调用子组件方法刷新下拉数据
			tableRefProcessRouteItemRoutieList.value.refreshComboData();
		}

	}
	else {
		stateProcessRoute.vmModel.Entity.ProductCode = '';
	}


}


//产品选择时
const OnProductChange= (value: any)  =>
{
	if (value)
	{ 
		const prod = stateProcessRoute.AllProducts.find(item => item.Value ===String(value));
		stateProcessRoute.vmModel.Entity.ProductCode = prod.Text;   
	}
	else{
		stateProcessRoute.vmModel.Entity.ProductCode='';
	}


}



// 在挂载前开始数据获取
onBeforeMount(() => {
	try {
		//stateProcessRoute.AllEquipments = [{Text:"s",Value:"s"}];
	} catch (err) {
		console.error('获取设备数据失败:', err)

	} finally {

	}
})



onMounted(() => {

	other.getSelectList('/api/ProcessManagement/ProcessRoute/GetProductionLine', [], false).then(x => { stateProcessRoute.AllProductLines = x });
	// other.getSelectList('/api/ProductManagement/ProductBom/GetProducts',[],false).then(x=>{stateProcessRoute.AllProducts = x});
	other.getSelectList('/api/ProcessManagement/ProcessRouteItem/GetProcessRoutes', [], false).then(x => { stateProcessRoute.AllRouties = x });
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};
// 暴露变量
defineExpose({

});
</script>

<style scoped lang="scss"></style>
